package org.example.k8s_cloudplatform.scheduler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.example.k8s_cloudplatform.entity.Pod;
import org.example.k8s_cloudplatform.entity.User;
import org.example.k8s_cloudplatform.service.PodService;
import org.example.k8s_cloudplatform.service.UserService;
import org.example.k8s_cloudplatform.utils.CheckUtil;
import org.example.k8s_cloudplatform.utils.DateUtil;
import org.example.k8s_cloudplatform.utils.EmailVerifyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ExpirationScheduler {

    @Resource
    private PodService podService;
    @Resource
    private UserService userService;

    private final Logger logger = LoggerFactory.getLogger(ExpirationScheduler.class);

    @Scheduled(cron = "0 0 0 * * ?")
    public void expirationCheck() {
        //查询pod的到期时间，距离到期时间小于3天，大于2天的发送邮件提醒
        Date now = new Date();
        LambdaQueryWrapper<Pod> podWrapper= Wrappers.lambdaQuery();
        podWrapper.eq(Pod::getStatus, 0);
        podWrapper.ge(Pod::getExpirationTime, DateUtil.dateAdd(now, 2));
        podWrapper.le(Pod::getExpirationTime, DateUtil.dateAdd(now, 3));

        List<Pod> podList = podService.list(podWrapper);
        for (Pod pod : podList) {
            //发送邮件提醒
            User user = userService.getById(pod.getUserID());
            if(user.getEmail()!=null&& CheckUtil.isEmail(user.getEmail())){
                //发送邮件
                try {
                    EmailVerifyUtil.sendMail(user.getEmail(),"山东大学Docker云平台通知",
                            "您的Pod(ID:"+pod.getId()+")将在三天内到期，如需保存请及时提交续租申请。<br>" +
                                    "如有疑问请联系平台管理员");
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("账号(ID:"+user.getID()+")邮件发送失败");
                }
            }
        }
        //小于一天的发送紧急邮件提醒
        podWrapper.clear();
        podWrapper.eq(Pod::getStatus, 0);
        podWrapper.ge(Pod::getExpirationTime, now);
        podWrapper.le(Pod::getExpirationTime, DateUtil.dateAdd(now, 1));

        podList = podService.list(podWrapper);
        for (Pod pod : podList) {
            //发送邮件提醒
            User user = userService.getById(pod.getUserID());
            if(user.getEmail()!=null&& CheckUtil.isEmail(user.getEmail())){
                //发送邮件
                try {
                    EmailVerifyUtil.sendMail(user.getEmail(),"山东大学Docker云平台通知【重要】",
                            "您的Pod(ID:"+pod.getId()+")将在一天内到期，<b>所有资源即将被释放</b>，如需保存请及时提交续租申请。<br>" +
                                    "如有疑问请联系平台管理员");
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("账号(ID:"+user.getID()+")邮件发送失败");
                }
            }
        }
        //已经过期，启动自动回收
        podWrapper.clear();
        podWrapper.eq(Pod::getStatus, 0);
        podWrapper.le(Pod::getExpirationTime, now);
        podList = podService.list(podWrapper);
        for (Pod pod : podList) {
            //回收pod
            podService.delete(pod.getId(),1);
            logger.info("Pod(ID:"+pod.getId()+")已到期，自动回收");
        }
    }
}
